package com.movieproject.src;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.movieproject.daos.MovieDAO;
import com.movieproject.daos.QueueDAO;
import com.movieproject.daos.UserDAO;
import com.movieproject.dataobjects.Movie;
import com.movieproject.dataobjects.MovieList;
import com.movieproject.dataobjects.User;

@Controller
@Transactional
public class QueueTest {
	@Autowired
	private MovieDAO movieDAO;

	@Autowired
	private UserDAO userDAO;

	@Autowired
	private QueueDAO queueDAO;

	@RequestMapping(value = "queuetest", method = RequestMethod.GET)
	public ModelAndView queueTest() throws InterruptedException {
		ModelAndView m = new ModelAndView();

		User user = userDAO.getAllUsers().get(0); //get a user and their queue
		ArrayList<Movie> queue = (ArrayList<Movie>) queueDAO.getUserQueue(user);
		
		//user = userDAO.getUserByEmail(user.getEmail());
		user = userDAO.getUser(user.getEmail());

		Collections.shuffle(queue); //do something to the queue
		queueDAO.updateUserQueue(user, queue);


		m.getModelMap().addAttribute("user", user);
		m.getModelMap().addAttribute("q1", queueDAO.getUserQueue(user));

		Movie movie = queueDAO.pollMovie(user); //poll a movie off the queue

		m.getModelMap().addAttribute("q2", queueDAO.getUserQueue(user));
		m.getModelMap().addAttribute("movie", movie);

		queueDAO.addMovie(user, movie); // add movie back in 

		m.getModelMap().addAttribute("q3", queueDAO.getUserQueue(user));
		
		ArrayList<Movie> searchresults = (ArrayList<Movie>) movieDAO.searchMovies("sandler wedding");
		m.getModelMap().addAttribute("searchresults", searchresults);
		m.getModelMap().addAttribute("size", searchresults.size());
		
		AppSecurity.generateResetCode();

		return m;
	}
	
	@RequestMapping(value = "queueEdit", method = RequestMethod.GET)
	public ModelAndView queueEdit(HttpServletRequest request) 
			throws InterruptedException {
		
	    ModelAndView m = new ModelAndView();

	    User user = userDAO.getUser(1); //get a user and their queue
	    m.getModelMap().addAttribute("user", user);
	    
	    List<Movie> newMovieList = queueDAO.getUserQueue(user);
	    List<Movie> arrayMovieList = new ArrayList<Movie>();
	    for (Movie mov : newMovieList) {
	    	arrayMovieList.add(mov);
		}
	    
	    MovieList movieList = new MovieList();
//	    movieList.setMovieList(newMovieList);
	    movieList.setMovieList(arrayMovieList);
	    m.getModelMap().addAttribute("editQueueForm", movieList);
	    
	    return m;
	}
	
	@RequestMapping(value = "queueEdit", method = RequestMethod.POST)
	public String updateUserQueue(@ModelAttribute("editQueueForm") MovieList movieList) 
			throws InterruptedException {
		
		List<Movie> movieUpdateArrayList = movieList.getMovieList();
		List<Movie> movieUpdateList = new ArrayList<Movie>();

//		for (int i = movieUpdateArrayList.size() - 1; i >= 0; i--) { 
//			movieUpdateList.add(movieUpdateArrayList.get(i));
//		}
		
		
//		List<Movie> movieTestList = new ArrayList<Movie>();
//		movieTestList.add(movieDAO.getMovie(1));
//		movieTestList.add(movieDAO.getMovie(2));
//		movieTestList.add(movieDAO.getMovie(3));
//		movieTestList.add(movieDAO.getMovie(4));
		
		
		User user = userDAO.getUser(1);
		queueDAO.updateUserQueue(user, movieUpdateArrayList);
		
		return "queueEdit";
	}
    	    

}
